package com.example.doctorserver.service.impl;

import com.example.doctorserver.dto.CiReportDTO;
import com.example.doctorserver.dto.CiReportDetailedDTO;
import com.example.doctorserver.mapper.*;
import com.example.doctorserver.pojo.Checkitem;
import com.example.doctorserver.pojo.Checkitemdetailed;
import com.example.doctorserver.pojo.Cidetailedreport;
import com.example.doctorserver.pojo.Cireport;
import com.example.doctorserver.service.CiReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CiReportServiceImpl implements CiReportService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CireportMapper cireportMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealdetailedMapper setmealdetailedMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Autowired
    private CidetailedreportMapper cidetailedreportMapper;

    @Autowired
    private CheckitemdetailedMapper checkitemdetailedMapper;


    @Override
    public List<CiReportDetailedDTO> listCiReport(Integer orderId) {

        //orderId查询smId
        int getsmId = ordersMapper.getsmId(orderId);

        List<Integer> getciId=setmealdetailedMapper.getciId(getsmId);

        List<CiReportDetailedDTO> ans=new ArrayList<>();


        int i=0;
        //查询orderId是否在cidetailedreport表中存在
        int count = cidetailedreportMapper.getCiReportByOrderld(orderId);
        if(count>0) {
            i=1;
        }


        for (Integer ciId : getciId) {

            //根据ciId查询checkitemdetailed表
            List<Checkitemdetailed> checkitemdetailed = checkitemdetailedMapper.selectByPrimaryKey(ciId);

            System.out.println(checkitemdetailed);

            // 创建一个用于存储Cidetailedreport对象的列表
            List<Cidetailedreport> cidetailedreports = new ArrayList<>();

            // 遍历查询结果，将数据插入到cidetailedreport表中
            for (Checkitemdetailed ch : checkitemdetailed) {
                // 创建一个新的Cidetailedreport对象
                Cidetailedreport cidetailedreport = new Cidetailedreport();

                // 将checkitemdetailed表中的数据设置到Cidetailedreport对象中

                cidetailedreport.setName(ch.getName());
                cidetailedreport.setUnit(ch.getUnit());
                cidetailedreport.setMinrange(ch.getMinrange());
                cidetailedreport.setMaxrange(ch.getMaxrange());
                cidetailedreport.setNormalValue(ch.getNormalValue());
                cidetailedreport.setNormalValueString(ch.getNormalValueString());
                cidetailedreport.setType(ch.getType());
                cidetailedreport.setCiId(ch.getCiId());
                cidetailedreport.setOrderId(orderId);
                cidetailedreport.setCdId(ch.getCdId());
                // 调用cidetailedreportMapper的insert方法将数据插入到cidetailedreport表中
                if (i==0) {
                    //如果orderId存在value为空，并且不插入
                    cidetailedreport.setIsError(0);
                    cidetailedreport.setValue("");
                    cidetailedreportMapper.insert(cidetailedreport);
                }else{
                    Cidetailedreport v=cidetailedreportMapper.getValue(cidetailedreport);
                    cidetailedreport.setValue(v.getValue());
                    cidetailedreport.setIsError(v.getIsError());
                }

                // 将Cidetailedreport对象添加到列表中
                cidetailedreports.add(cidetailedreport);
            }

            //根据ciId查询checkitem表中的ciName
            String ciName = checkitemMapper.getCiName(ciId);
            //checkitemdetailed不为空，将ciId，ciName，checkitemdetailed放入ans列表中
            if (checkitemdetailed != null) {
            CiReportDetailedDTO ci = new CiReportDetailedDTO();
            ci.setCiId(ciId);
            ci.setCiName(ciName);
            ci.setCidrList(cidetailedreports);
            ans.add(ci);
            }
        }

        return ans;

    }
}
